package com.hroa.web.interceptor;

import com.hroa.web.exception.BusinessException;
import com.hroa.web.vo.ResponseEntity;
import com.hroa.web.vo.StatusInfo;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by whz on 2014/12/23.
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    protected Logger logger = Logger.getLogger(getClass());

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResponseEntity<Void> handle(Exception ex) {
        logger.error("", ex);
        ResponseEntity<Void> result = new ResponseEntity<>();
        if (ex instanceof BusinessException) {
            String errorCode = ((BusinessException) ex).getErrorCode();
            result.setCode(errorCode);
            result.setMessage(((BusinessException) ex).getMessage());
        } else {
            result.setCode(StatusInfo.REQUEST_FAILURE);
            result.setMessage(StatusInfo.REQUEST_FAILURE_MSG);
        }
        return result;
    }
}
